/**
 * @Description: diff指令
 * @author Beon
 * @date 2021/1/4
*/
import * as childProcess from 'child_process';
import {ErrorCatch} from '../decorators/errorCatch';
import '../utils/consoleColor';

export class Diff{

    /**
     * 判断本地是否有修改
     * @param {String} repoPath 执行路径
     * @returns {Promise<void>}
     */
    @ErrorCatch.errorExit('文件比对失败，请查看报错原因')
    static async isClean (repoPath?: string): Promise<boolean> {
        const diff = await childProcess.execSync('git diff --no-ext-diff --name-only && git diff --no-ext-diff --cached --name-only', { cwd: repoPath });
        return !!diff.toString().trim().length;
    }

    /**
     * 如果为空抛出异常
     * @param {String} repoPath 执行路径
     * @returns {Promise<void>}
     */
    @ErrorCatch.errorExit('没有文件进行过修改')
    static async isCleanError (repoPath?: string): Promise<void> {
        if (!await this.isClean(repoPath)) {
            throw Error();
        }
    }
}
